﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunAward : EnemyBase
{
    [System.Serializable]
    public class Config
    {
        public Sprite sprite;
        public float intervalTime = 1f;
        public bool interactive = false;
    }

    public ABullet.BulletType bulletType = ABullet.BulletType.M;
    public Config[] configSequence;
    public GameObject gunPrefab;

    public bool working = false;


    private float lastTime;
    private int index = 0;
    private SpriteRenderer sr;
    private Collider2D collider2d;

    private void Awake()
    {
    }
    void Start()
    {
        health = 0;
        lastTime = Time.time;
        sr = GetComponent<SpriteRenderer>();
        collider2d = GetComponent<Collider2D>();
        //StartWork();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
        if (Utility.InCameraViewport(Camera.main, transform.position + (Vector3.right * 1.0f))
            && Utility.InCameraViewport(Camera.main, transform.position + (Vector3.left * 1.0f)))
        {
            StartWork();
        }
        else
        {
            StopWork();
        }
        if (configSequence.Length > 0)
        {
            if (working)
            {
                Config config = configSequence[index];
                sr.sprite = config.sprite;
                float t = Time.time - lastTime;
                if (t >= config.intervalTime)
                {
                    lastTime = Time.time;
                    index++;
                    if (index >= configSequence.Length)
                    {
                        index = 0;
                    }
                }
                collider2d.enabled = config.interactive;
            }
            else
            {
                index = 0;
                Config config = configSequence[index];
                sr.sprite = config.sprite;
                collider2d.enabled = false;
            }
        }

    }

    public void StartWork()
    {
        if (!working)
        {
            working = true;
        }
    }
    public void StopWork()
    {
        if (working)
        {
            working = false;
        }
    }

    public override float Damage(GameObject source, float value)
    {
        Die();
        return 0;
    }

    public override void Die()
    {
        ThrowGun();
        Boom();
    }

    public override void Run()
    {
        StartWork();
    }
    public override void Stop()
    {
        StopWork();
    }
    private void Boom()
    {
        if (boomPrefab != null)
        {
            Instantiate(boomPrefab, transform.position, Quaternion.identity);
            Constant.Sounds.EnemyDeath1.PlayOneShot();
        }
        Destroy(gameObject);
    }
    private void ThrowGun()
    {
        if (gunPrefab != null)
        {
            GameObject gunObj = Instantiate(gunPrefab, transform.position, Quaternion.identity);
            if (gunObj != null)
            {
                Gun gun = gunObj.GetComponent<Gun>();
                gun.SetBulletType(bulletType);
                gun.Throw();
            }
        }
    }

    public override bool IsDie()
    {
        throw new System.NotImplementedException();
    }
}
